<?php
/**
 * @title 100208. 统计前后缀下标对 II
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * @title 字典树
 * @author start2004
 * @since 2024-02-18 Created
 */
class WordTree {
    public $k, $cnt, $next;
    function __construct($k = '', $cnt = 0, $next = []){
        $this->k = $k;
        $this->cnt = $cnt;
        $this->next = $next;
    }
}

class Solution {

    /**
     * @param String[] $words
     * @return Integer
     */
    function countPrefixSuffixPairs($words) {
        /**
         * @since 2024-02-18 以整数形式，返回满足 i < j 且 isPrefixAndSuffix(words[i], words[j]) 为 true 的下标对 (i, j) 的 数量
         */
        $pairCount = 0;

        /**
         * @since 2024-02-18 字典树
         */
        $wordTree = new WordTree();

        /**
         * @since 2024-02-18 遍历单词数组
         */
        $wordCount = count($words);
        for($i=$wordCount-1; $i>=0; $i--){
            $word = $words[$i];
            $wordLength = strlen($word);
            $t = $wordTree;

            /**
             * @since 2024-02-18 遍历单词
             */
            for($s=0, $e=$wordLength-1; $s<$wordLength; $s++, $e--){
                $k = $word[$s] ."-". $word[$e];
                if(!isset($t->next[$k])){
                    $t->next[$k] = new WordTree($k);
                } else {}
                $t = $t->next[$k];
                $t->cnt++;
            }
            $pairCount = $pairCount + $t->cnt-1;
        }

        /**
         * @return Integer
         */
        return $pairCount;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202402/2024.02.18_5.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
